package com.objecteffects.ilmp.web.utils;

import java.lang.annotation.Annotation;

import com.objecteffects.ilmp.web.action.BaseActionBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Rusty Wright
 */
public class CheckLoginRequired {
    private static final Logger LOG = LoggerFactory
        .getLogger(CheckLoginRequired.class);

    @SuppressWarnings("static-method")
    public boolean isLoginRequired(final Class<? extends BaseActionBean> clazz) {
        final Annotation[] annotations = clazz.getAnnotations();

        String shortName = "";

        if (CheckLoginRequired.LOG.isDebugEnabled()) {
            final String canonicalName = clazz.getCanonicalName();
            final int lastDot = canonicalName.lastIndexOf(".");
            shortName = canonicalName.substring(lastDot + 1);
        }

        for (final Annotation annotation : annotations) {
            if (annotation instanceof LoginRequired) {
                CheckLoginRequired.LOG.debug("returning true: {}", shortName);

                return true;
            }
        }

        CheckLoginRequired.LOG.debug("returning false: {}", shortName);

        return false;
    }
}
